<?php
class Nilai_raport extends CI_Controller {

	function __construct()
	{
		parent::__construct();	
		$this->load->helper('flexigrid');
		$this->load->helper('format');
		$this->load->library('flexigrid');
		$this->load->library('form_validation');
		$this->load->model('master_model');
		$this->load->model('pelajaran_model');
		$this->load->model('penilaian_model');
		$this->load->model('presensi_model');
	}
	
	function index($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $kode_mp, $act='')
	{
		
		/*
		 * 0 - display name
		 * 1 - width
		 * 2 - sortable
		 * 3 - align
		 * 4 - searchable (2 -> yes and default, 1 -> yes, 0 -> no.)
		 */
		$nilai = $this->penilaian_model->getNilai($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $kode_mp)->num_rows();
		if($nilai==0){
			echo "<script type='text/javascript'>
				alert ('Silakan mengisi Nilai Siswa terlebih dahulu.');
				location.href='".base_url()."index.php/nilai_siswa/index/".$id_sekolah."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester."/".$kode_mp."';
			</script>";
		}
		$nilai_raport = $this->penilaian_model->getNilairaport($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $kode_mp);
		$jml_kolom=$this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
		$colModel['no'] = array('No',40,TRUE,'center',0);
		if($this->session->userdata('roleid')=='0') //diknas
			$colModel['NAMA_SEKOLAH'] = array('Nama Sekolah',100,TRUE,'center',1);
		$colModel['NISN'] = array('NISN',100,TRUE,'center',1);
		$colModel['NAMA_LENGKAP'] = array('Nama Lengkap',300,TRUE,'center',1);
		$jml_prosen=0;foreach($jml_kolom->result() as $row){
			$colModel[$row->KODE_UJIAN] = array($row->NAMA_UJIAN,100,TRUE,'center',1);
			$jml_prosen += $row->PROSENTASE;
		}
		$colModel['rata-rata'] = array('Nilai Rata-Rata',100,FALSE,'center',0);
		$colModel['nilai_raport'] = array('Nilai Raport',100,FALSE,'center',0);
		
		/*
		 * Aditional Parameters
		 */
		$gridParams = array(
		'width' => 'auto',
		'height' => '345',
		'rp' => 10,
		'rpOptions' => '[10,15,20,25,40]',
		'pagestat' => 'Displaying: {from} to {to} of {total} items.',
		'blockOpacity' => 0.5,
		'title' => 'Daftar Siswa >> Input Nilai Raport'
		);
		
		/*
		 * 0 - display name
		 * 1 - bclass
		 * 2 - onpress
		 */
		
		if($jml_prosen==100){
			if($nilai_raport->num_rows() == 0)	
				$buttons[] = array('Tambah Nilai','add','org_js');
			else{
				$buttons[] = array('Edit Nilai','edit','org_js');
				$buttons[] = array('Delete','delete','org_js');
			}
		}else{
			$data['header_info'] = '<div class="ret_error">Prosentase Nilai Ujian Belum 100%, Silakan Tambah Nilai Ujian Terlebih dahulu.</div>';
		}
		$buttons[] = array('separator');
		$buttons[] = array('Back to Pelajaran Guru','back','org_js');
		
		//Build js
		//View helpers/flexigrid_helper.php for more information about the params on this function
		$grid_js = build_grid_js('flex1',site_url("/nilai_raport/listnilairaport_grid/".$this->session->userdata('unitid')."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester."/".$kode_mp),$colModel,'no','asc',$gridParams,$buttons);
		
		$data['header_info'] = "
		<div><blink><a title='klik untuk melihat Nilai Raport Per Siswa' href='".base_url()."index.php/nilai_raport_persiswa/index/".$this->session->userdata('unitid')."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester."'><font size='3' color='red'><b>Lihat Nilai Rapor Per Siswa</b></font></a></blink></div>
		";
		
		if($this->session->flashdata('suc_msg'))
			$data['header_info'] .= '<div class="ret_success">'.$this->session->flashdata('suc_msg').'</div>';
			
		$data['title'] = "Contoh Flexigrid";
		$data['info'] = "Flexigrid";
		$data['js_grid'] = $grid_js;
		$data['version'] = "0.36";
		$data['download_file'] = "Flexigrid_CI_v0.36.rar";
		
		$data['added_js'] = 
			"<script type='text/javascript'>
			function org_js(com,grid)
			{
				if (com=='Select All')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Deselect All')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah Nilai')
				{
					 location.href='".base_url()."index.php/nilai_raport/input_nilairaport/".$this->session->userdata('unitid')."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester."/".$kode_mp."'; 
				}
				
				if (com=='Edit Nilai')
				{
					 location.href='".base_url()."index.php/nilai_raport/edit_nilairaport/".$this->session->userdata('unitid')."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester."/".$kode_mp."'; 
				}
				
				if (com=='Back to Pelajaran Guru')
				{
					location.href='".base_url()."index.php/nilai_siswa_raport/'; 
				}
				
				if (com=='Delete')
				{
					var del=confirm('Apakah Anda yakin akan menghapus Semua Nilai Raport Untuk Mata Pelajaran Ini ?');
					if (del){
						location.href='".base_url()."index.php/nilai_raport/delete_nilairaport/".$this->session->userdata('unitid')."/".$id_ta."/".$id_tingkat."/".$id_kelas."/".$id_semester."/".$kode_mp."';
					}
				}          
			} 
			</script>
			";
			
		$data['content'] = $this->load->view('grid',$data,true);
		$this->load->view('main',$data);
	}
	
	function listnilairaport_grid($id_sekolah,$id_ta,$id_tingkat,$id_kelas,$id_smt,$kode_mp)
	{	
	
		//List of all fields that can be sortable. This is Optional.
		//This prevents that a user sorts by a column that we dont want him to access, or that doesnt exist, preventing errors.
		$valid_fields = array('NAMA_TA','NISN','NAMA_LENGKAP','KET');
		
		$this->flexigrid->validate_post('a.NISN','asc',$valid_fields);

		$records = $this->penilaian_model->get_siswa_kelas($id_sekolah,$id_ta,$id_tingkat,$id_kelas);
		
		$this->output->set_header($this->config->item('json_header'));
		$no = 0;
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		$jml_kolom=$this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
		$nilai = $this->penilaian_model->getNilai($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_smt, $kode_mp);
		$nilai_raport = $this->penilaian_model->getNilairaport($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_smt, $kode_mp);
		
		$pembanding = array();$pembanding[0]='';$j=1;
		$arr_jml_nilai = array();
		
		//nilai siswa
		foreach($nilai->result() as $row_nilai){
			$arr_nilai[$row_nilai->NISN][$row_nilai->ID_JENIS_UJIAN] = $row_nilai->NILAI;
			$pembanding[$j] = $row_nilai->NISN;
			if($pembanding[$j-1] != $pembanding[$j]){
				$rata_nilai = 0; 
				$rata_nilai += $row_nilai->NILAI;
				$arr_jml_nilai[$row_nilai->NISN][1] = $rata_nilai;
			}else{
				$rata_nilai += $row_nilai->NILAI;
				$arr_jml_nilai[$row_nilai->NISN][1] = $rata_nilai;
			}
		$j++;
		}
		//nilai raport
		foreach($nilai_raport->result() as $row_nilai_raport){
			$arr_nilai_raport[$row_nilai_raport->NISN] = $row_nilai_raport->NILAI;
		}
		
		foreach ($records['records']->result() as $row)
		{
			if( !empty($arr_nilai_raport[$row->NISN]) and !empty($arr_jml_nilai[$row->NISN][1])){
				$arr_tambahan = array();
				$arr_asli = array ($row->NISN, $no+1,
					'<div align="left">'.$row->NISN.'</div>',  
					'<div align="left">'.$row->NAMA_LENGKAP.'</div>',
					'<div align="center">'. $arr_jml_nilai[$row->NISN][1]/$jml_kolom->num_rows() .'</div>',
					'<div align="center">'. $arr_nilai_raport[$row->NISN] .'</div>'
					);
				foreach($jml_kolom->result() as $rowj){
					$arr_tambahan[] = '<div align="center">'.$arr_nilai[$row->NISN][$rowj->ID_JENIS_UJIAN].'</div>';
				}
				array_splice( $arr_asli, 4, 0, $arr_tambahan );
				$record_items[] = $arr_asli;
				$no++;
			}else{
				if(!empty($arr_jml_nilai[$row->NISN][1])){
					$arr_tambahan = array();
					$arr_asli = array ($row->NISN, $no+1,
						'<div align="left">'.$row->NISN.'</div>',  
						'<div align="left">'.$row->NAMA_LENGKAP.'</div>',
						'<div align="center">'. $arr_jml_nilai[$row->NISN][1]/$jml_kolom->num_rows() .'</div>',
						'<div align="center"></div>'
						);
					foreach($jml_kolom->result() as $rowj){
						$arr_tambahan[] = '<div align="center">'.$arr_nilai[$row->NISN][$rowj->ID_JENIS_UJIAN].'</div>';
					}
					array_splice( $arr_asli, 4, 0, $arr_tambahan );
					$record_items[] = $arr_asli;
					$no++;
				}else{
					$arr_tambahan = array();
					$arr_asli = array ($row->NISN, $no+1,
						'<div align="left">'.$row->NISN.'</div>',  
						'<div align="left">'.$row->NAMA_LENGKAP.'</div>',
						'<div align="center"></div>',
						'<div align="center"></div>'
						);
					foreach($jml_kolom->result() as $rowj){
						$arr_tambahan[] = '<div align="center"></div>';
					}
					array_splice( $arr_asli, 4, 0, $arr_tambahan );
					$record_items[] = $arr_asli;
					$no++;
				}
			}
		}
		//Print please
		if(isset($record_items))
				$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
			else
				$this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	
	function cek_validasi() {	
			// Setting Rules
			$this->form_validation->set_rules('pelajaran', 'Pelajaran', 'required');
			if($this->session->userdata('roleid')=='0') //diknas
				$this->form_validation->set_rules('nama_sekolah', 'Nama Sekolah', 'required');
			
			// Setting Error Message
			$this->form_validation->set_message('required', 'Kolom %s harus diisi!!');
			
			// Setting Delimiter
			$this->form_validation->set_error_delimiters('<li class="error">', '</li>');
			return $this->form_validation->run();
	}
	
	function input_nilairaport($id_sekolah,$id_ta,$id_tingkat,$id_kelas,$id_smt,$kode_mp)
    {
		$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
		$data['paramid'] = $id_sekolah."#".$id_ta."#".$id_tingkat."#".$id_kelas."#".$id_smt."#".$kode_mp;
		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'new';
			$data['r_tingkat'] = $this->pelajaran_model->getTingkat($id_sekolah);
			$data['r_kelas'] = $this->pelajaran_model->getKelas($id_sekolah);
			$data['r_semester'] = $this->pelajaran_model->getSmt($id_sekolah);
			$data['r_pelajaran'] = $this->pelajaran_model->get_pelajaranID($id_sekolah,$id_ta,$kode_mp);
			$data['r_daftarsiswa'] = $this->presensi_model->get_siswa_kelas($id_sekolah, $data['tahunajaran']->row()->ID_TA, $id_tingkat, $id_kelas);
			$data['jml_kolom'] = $this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
			$data['rs_nilaisiswa'] = $this->penilaian_model->getNilai($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_smt, $kode_mp);
			
			$data['content'] = $this->load->view('form_nilai_raport',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->insert_nilairaport($id_sekolah,$id_ta,$id_tingkat,$id_kelas,$id_smt,$kode_mp);
		}            
    }
	
	function insert_nilairaport($id_sekolah,$id_ta,$id_tingkat,$id_kelas,$id_smt,$kode_mp){
		$siswa_kelas = $this->presensi_model->get_siswa_kelas($id_sekolah, $id_ta, $id_tingkat, $id_kelas);
		$jml_kolom=$this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
		foreach($siswa_kelas->result() as $row){ //NISN
			$nilai_angka = $this->input->post('nilairaport_'.$row->NISN);
			$rs_huruf = $this->penilaian_model->cari_grade($kode_mp,$nilai_angka)->row();
			$nilai_huruf = $rs_huruf->GRADE;
			// foreach($jml_kolom->result() as $row_col){ //KODE_UJIAN
				$data = array(
					'TAHUN_AJARAN'			=> $id_ta,
					'ID_SEKOLAH'			=> $id_sekolah,
					'ID_TINGKAT'			=> $this->input->post('tingkat'),	
					'ID_KELAS'				=> $this->input->post('kelas'),	
					'ID_SEMESTER'			=> $this->input->post('semester'),	
					'KODE_MATA_PELAJARAN'	=> $this->input->post('pelajaran'),	
					'NISN'		 			=> $row->NISN,
					'NILAI'					=> $this->input->post('nilairaport_'.$row->NISN),
					'NILAI_HURUF'			=> $nilai_huruf,
					'KETERANGAN' 			=> $this->input->post('keterangan_'.$row->NISN)
				);
				$this->penilaian_model->insert_nilairaport($data);
			// }
		}
		$this->session->set_flashdata('suc_msg', 'Data berhasil disimpan.');
		redirect('nilai_raport/index/'.$id_sekolah.'/'.$id_ta.'/'.$id_tingkat.'/'.$id_kelas.'/'.$id_smt.'/'.$kode_mp); 
	}
	
	function edit_nilairaport($id_sekolah,$id_ta,$id_tingkat, $id_kelas, $id_semester, $kode_mp)
    {	
		$data['tahunajaran'] = $this->pelajaran_model->getTa($id_sekolah);
		$data['paramid'] = $id_sekolah."#".$id_ta."#".$id_tingkat."#".$id_kelas."#".$id_semester."#".$kode_mp;
 		if ($this->cek_validasi() == FALSE){
			$data['status'] = 'edit';
			$data['r_tingkat'] = $this->pelajaran_model->getTingkat($id_sekolah);
			$data['r_kelas'] = $this->pelajaran_model->getKelas($id_sekolah);
			$data['r_semester'] = $this->pelajaran_model->getSmt($id_sekolah);
			$data['r_pelajaran'] = $this->pelajaran_model->get_pelajaranID($id_sekolah,$id_ta,$kode_mp);
			$data['r_daftarsiswa'] = $this->presensi_model->get_siswa_kelas($id_sekolah, $data['tahunajaran']->row()->ID_TA, $id_tingkat, $id_kelas);
			$data['jml_kolom'] = $this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
			$data['rs_nilaisiswa'] = $this->penilaian_model->getNilai($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $kode_mp);
			
			$data['rs_nilairaport'] = $this->penilaian_model->getNilairaport($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $kode_mp);
			$data['content'] = $this->load->view('form_nilai_raport',$data,true);
			$this->load->view('main',$data);
		}
		else{
			$this->update_nilairaport($id_sekolah,$id_ta,$id_tingkat, $id_kelas, $id_semester, $kode_mp);
		}            
    }
	
	function update_nilairaport($id_sekolah,$id_ta,$id_tingkat, $id_kelas, $id_semester, $kode_mp){
		$this->penilaian_model->delete_nilairaport($id_sekolah, $id_ta,$id_tingkat,$id_kelas,$id_semester,$kode_mp);
		$siswa_kelas = $this->presensi_model->get_siswa_kelas($id_sekolah, $id_ta, $id_tingkat, $id_kelas);
		$jml_kolom=$this->penilaian_model->getUjian($id_sekolah, $id_ta, $kode_mp);
		foreach($siswa_kelas->result() as $row){
			$nilai_angka = $this->input->post('nilairaport_'.$row->NISN);
			$rs_huruf = $this->penilaian_model->cari_grade($kode_mp,$nilai_angka)->row();
			$nilai_huruf = $rs_huruf->GRADE;
			
			// foreach($jml_kolom->result() as $row_col){
				$data = array(
					'TAHUN_AJARAN'			=> $id_ta,
					'ID_SEKOLAH'			=> $id_sekolah,
					'ID_TINGKAT'			=> $this->input->post('tingkat'),	
					'ID_KELAS'				=> $this->input->post('kelas'),	
					'ID_SEMESTER'			=> $this->input->post('semester'),	
					'KODE_MATA_PELAJARAN'	=> $this->input->post('pelajaran'),	
					'NISN'		 			=> $row->NISN,
					'NILAI'					=> $this->input->post('nilairaport_'.$row->NISN),
					'NILAI_HURUF'			=> $nilai_huruf,
					'KETERANGAN' 			=> $this->input->post('keterangan_'.$row->NISN)
				);
				$this->penilaian_model->insert_nilairaport($data);
			// }
		}
	
		$this->session->set_flashdata('suc_msg', 'Data berhasil diupdate.');
		redirect('nilai_raport/index/'.$id_sekolah.'/'.$id_ta.'/'.$id_tingkat.'/'.$id_kelas.'/'.$id_semester.'/'.$kode_mp); 
	}
	
	//Delete
	function delete_nilairaport($id_sekolah, $id_ta,$id_tingkat,$id_kelas,$id_semester,$kode_mp)
	{
		$this->penilaian_model->delete_nilairaport($id_sekolah, $id_ta,$id_tingkat,$id_kelas,$id_semester,$kode_mp);
		$this->session->set_flashdata('suc_msg', 'Data berhasil dihapus.');
		redirect('nilai_raport/index/'.$id_sekolah.'/'.$id_ta.'/'.$id_tingkat.'/'.$id_kelas.'/'.$id_semester.'/'.$kode_mp); 
	}
	
}
?>
